#include "scrollarea.h"
#include <qevent.h>
#include <qscrollbar.h>
#include <qmenu.h>
#include <QtOpenGL/QtOpenGL>
ScrollArea::ScrollArea(QWidget *parent) :
	QGraphicsView(parent)
{
	intime=0;
	this->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
	QPalette p=viewport()->palette();
	//setBackgroundRole();
	p.setColor(QPalette::Background,QColor(45,46,47));
	viewport()->setPalette(p);rightKey=0;scrolled=0;cmenu=0;
	viewport()->setBackgroundRole(QPalette::Background);
	//timer.setSingleShot(1);
}
//void ScrollArea::mouseDoubleClickEvent(QMouseEvent *e)
//{
	////qdebug()<<"doubleclick";
//	if(e->button()==Qt::LeftButton)
//		emit next();
//}

void ScrollArea::mousePressEvent(QMouseEvent *e)
{
	if(e->button()==Qt::LeftButton)
	{
		//if(cmenu && cmenu->isModal())
		//	return;
		if(e->x()<viewport()->width()*3/7)
			emit prev();
		else if(e->x()>viewport()->width()*4/7)
			emit next();
	}else if(e->button()==Qt::RightButton)
	{
		//qdebug()<<"rightkeypress";
		rightKey=1;
		//QScrollArea::mousePressEvent(e);
	}
}
void ScrollArea::mouseReleaseEvent(QMouseEvent *e)
{
	//qdebug()<<"rightkeypress";
	if(e->button()==Qt::RightButton)
	{
		rightKey=0;
		if(scrolled)
		{
			scrolled=0;return;
		}
		emit showCmenu();
		//QScrollArea::mousePressEvent(e);
	}
}

void ScrollArea::wheelEvent(QWheelEvent *e)
{
	//if(Qt::k)
	if(e->modifiers() & Qt::ControlModifier)
	{
		emit sscale(e->delta());
	}else if((e->modifiers() & Qt::ShiftModifier) || rightKey)
	{
		if(e->delta()>0)
			emit prev();
		else
			emit next();
		if(rightKey)
			scrolled=1;
	}else
	{
		//if(!intime && e->delta()<0 && this->verticalScrollBar()->value()==this->verticalScrollBar()->maximum())
		//	emit next();
		//else if(!intime && e->delta()>0 && this->verticalScrollBar()->value()==0)
		//	emit prev();
		//else
		//{
		//	if(!intime)
		//	{
		//		intime=1;
		//		QTimer::singleShot(500,this,SLOT(timeout()));
		//	}
		//}
	}

}
void ScrollArea::setcmenu(QMenu *p)
{
	cmenu=p;
}
void ScrollArea::timeout()
{
	intime=0;
}
